<?php

class IndexController extends Zend_Controller_Action
{

    /**
     * @var Cms_Model_Administrator
     */
    protected $_administratorModel;

    /**
     * @var Cms_Service_Authentication
     */
    protected $_authService;

    /**
     * @var array
     */
    protected $_forms = array();

     public function init()
    {

        // get the administrator model
        $this->_administratorModel = new Cms_Model_Administrator();

        // get authentication service
        $this->_authService = new Cms_Service_Authentication();

        // add login form
        $this->view->loginForm = $this->getLoginForm();

    }

    public function indexAction()
    {
    
        //if user haven't logged in - forward to the login page
        if (!$this->_authService->getAuth()->hasIdentity()) {
            $this->_forward('login');
        }
    }

     public function loginAction()
    {

         $request = $this->getRequest();
     
         if($request->isPost()){
             
              if (!$this->_forms['login']->isValid($request->getPost())) {
                if (count($this->_forms['login']->getErrors('token')) > 0) {
                    return $this->render('login');
                }    

                return $this->render('login');
            }

                if($this->_authService->authenticate($this->_forms['login']->getValues())){

                    $administratorInfo = $this->_administratorModel->getAdministratorById($this->_authService->getIdentity()->adminId);

                    //if administrator account is not enabled, clear session and redirect to login page
                    if($administratorInfo->isEnabled()){
                        //set administrator last login
                        $this->_administratorModel->setAdministratorLastLogin($administratorInfo);

                        if(null !== $this->_helper->redirectUrl()){
                                    $this->_redirect($this->_helper->redirectUrl(), array('prependBase'=>false));
                                    }else{
                                        $this->_helper->redirector->gotoRoute(array(),'cmsHome');
                                    }
                    }else{
                        $this->_authService->clear();
                        $this->_helper->flashMessenger->addMessage("Your account is not enabled for login.");
                        $this->_helper->redirector->gotoRoute(array(),'cmsLogin');
                    }
              
              }else{
                   $this->_helper->flashMessenger->addMessage('Invalid login please try again.');
                   $this->_helper->redirector->gotoRoute(array(),'cmsLogin');
              }
            
           }

        if ($this->_helper->flashMessenger->hasMessages()) {
            $this->view->message = $this->_helper->flashMessenger->getMessages();
        }


    }

     public function logoutAction()
    {
        $this->_authService->clear();
        $this->_helper->flashMessenger->addMessage("You have successfully signed out from your account.");
        $this->_helper->redirector->gotoRoute(array(),'cmsLogin');
    }

     
     public function getLoginForm()
    {
        $urlHelper = $this->_helper->getHelper('url');
        
        $this->_forms['login'] = $this->_administratorModel->getForm('administratorAdministratorLogin');
        $this->_forms['login']->setAction($urlHelper->url(array(),'cmsLogin').$this->_helper->redirectUrl(true));
        $this->_forms['login']->setMethod('post');
      
        return $this->_forms['login'];
    }

}

